//
//  GreatProductioVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/4/23.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "GreatProductioVC.h"
#import "signUpCollectionCell.h"

@interface GreatProductioVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;
@property (strong, nonatomic) IBOutlet UITextField *search_field;
@property (strong, nonatomic) IBOutlet UIButton *search_btn;
@property (nonatomic, strong) RequestServices *service;

@end

@implementation GreatProductioVC
{
    NSInteger page_index;//页码
    NSMutableDictionary *statusDic;
    NSMutableArray *listArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优秀作品";
    [self.backKuang setHidden:YES];
    statusDic = [NSMutableDictionary dictionary];
    listArr = [NSMutableArray array];
    
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    [self.collection_view registerNib:[UINib nibWithNibName:@"signUpCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"signUpCollectionCell"];
    
    NSAttributedString *attrString1 = [[NSAttributedString alloc] initWithString:@"   输入关键字搜索" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:self.search_field.font}];
    self.search_field.attributedPlaceholder = attrString1;
     self.search_field.delegate = self;
     self.search_field.tag = 6666;
     self.search_btn.titleLabel.font = self.search_field.font = AutoLabFont2;
    APPViewBorderRadius(self.search_field, 10, 1, Base_lineColor);
    //点击搜索
     [self.search_btn addTarget:self action:@selector(searchHotWord) forControlEvents:UIControlEventTouchUpInside];
    [self loadRequest];

}

-(void)loadRequest{
    self.service = [[RequestServices alloc]initWithDelegate:self];
    self.service.view = self.view;
    page_index = 1;
    [self headerRefresh];
    __weak __typeof(self)weakSelf = self;
       //左划
       self.collection_view.mj_trailer =[MJRefreshTrailer trailerWithRefreshingBlock:^{
           [weakSelf footerRefresh];
       }];
       self.collection_view.mj_trailer.hidden = YES;
}
- (void)headerRefresh
{
    self.service.view = self.view;
    page_index = 1;
    [self.service getGoodProductionList:self.typeID type:self.type current:page_index size:10 keyword:statusDic[@"hotName"] tag:8000];
}

- (void)footerRefresh
{
    page_index++;
    [self.service getGoodProductionList:self.typeID type:self.type current:page_index size:10 keyword:statusDic[@"hotName"] tag:8001];
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    if (tag == 8000 || tag == 8001) {
        [self.collection_view.mj_trailer endRefreshing];
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
        if (tag == 8000) {
                listArr = [AliefuModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        }else {
            [listArr addObjectsFromArray:[AliefuModel mj_objectArrayWithKeyValuesArray:tempArr]];
        }
        
        [self.collection_view reloadData];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self ShowNewMsg:error.domain];
    [self.collection_view.mj_trailer endRefreshing];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self searchHotWord];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text == nil) {
        //如果为空
        [statusDic setValue:@"" forKey:@"hotName"];
    }else{
        [statusDic setValue:textField.text forKey:@"hotName"];
    }
}

//点击搜索按钮
-(void)searchHotWord{
    UITextField *textField = [self.view viewWithTag:6666];
    [textField resignFirstResponder];
    //输入不一样再添加
    if (![textField.text isEqualToString:statusDic[@"hotName"]]) {
        [statusDic setValue:textField.text forKey:@"hotName"];
    }
    if ([statusDic[@"hotName"] CheckNSStringData]) {
        [self headerRefresh];
    }else{
        [SVProgressHUD showErrorWithStatus:@"请输入关键字搜索"];
    }
}


#pragma mark ----- collectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return listArr.count;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(collectionView.frame.size.width/3,collectionView.frame.size.height * 0.9);
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    signUpCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signUpCollectionCell" forIndexPath:indexPath];
    cell.model = listArr[indexPath.item];

    [cell.status_btn setHidden:YES];
    [cell.clock_img setHidden:YES];
    [cell.join_btn setBackgroundImage:[UIImage imageNamed:@"aliefu_btn"] forState:UIControlStateNormal];
    [cell.join_btn setTitle:@"点击进入" forState:UIControlStateNormal];
    //点击进入
    cell.join_btn.tag = indexPath.item + 1000;
    [cell.join_btn addTarget:self action:@selector(comeToInfor:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        //点击热词 获取列表页
//        SearchWordModel *model = listLeft[indexPath.row];
//        [statusDic setValue:model.hotName forKey:@"hotName"];
}

//四周边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(20, 20, 20, 20);
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
